/* -------------------------------------------------------------------------
 *
 * memnodes.h
 *	  openGauss memory context node definitions.
 *
 * Portions Copyright (c) 2020 Huawei Technologies Co.,Ltd.
 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 * Portions Copyright (c) 2010-2012 Postgres-XC Development Group
 *
 * $PostgreSQL: pgsql/src/include/nodes/memnodes.h,v 1.34 2008/01/01 19:45:58 momjian Exp $
 *
 * -------------------------------------------------------------------------
 */
#ifndef MEMNODES_H
#define MEMNODES_H

#include "gtm/gtm_lock.h"

/*
 * MemoryContext
 *		A logical context in which memory allocations occur.
 *
 * MemoryContext itself is an abstract type that can have multiple
 * implementations, though for now we have only AllocSetContext.
 * The function pointers in MemoryContextMethods define one specific
 * implementation of MemoryContext --- they are a virtual function table
 * in C++ terms.
 *
 * Note: for largely historical reasons, typedef MemoryContext is a pointer
 * to the context struct rather than the struct type itself.
 */

typedef struct MemoryContextMethods {
    void* (*alloc)(MemoryContext context, Size size);
    /* call this free_p in case someone #define's free() */
    void (*free_p)(MemoryContext context, void* pointer);
    void* (*realloc)(MemoryContext context, void* pointer, Size size);
    void (*init)(MemoryContext context);
    void (*reset)(MemoryContext context);
    void (*delete_context)(MemoryContext context);
    Size (*get_chunk_space)(MemoryContext context, void* pointer);
    bool (*is_empty)(MemoryContext context);
    void (*stats)(MemoryContext context, int level);
#ifdef MEMORY_CONTEXT_CHECKING
    void (*check)(MemoryContext context);
#endif
} MemoryContextMethods;

typedef struct MemoryContextData {
    MemoryContextMethods* methods; /* virtual function table */
    MemoryContext parent;          /* NULL if no parent (toplevel context) */
    MemoryContext firstchild;      /* head of linked list of children */
    MemoryContext nextchild;       /* next child of same parent */
    char* name;                    /* context name (just for debugging) */
    bool is_shared;                /* context is shared by threads */
    GTM_RWLock lock;               /* lock to protect members if the context is shared */
} MemoryContextData;

#define MemoryContextIsShared(context) (((MemoryContextData*)(context))->is_shared)

#define MemoryContextLock(context) (GTM_RWLockAcquire(&((MemoryContextData*)(context))->lock, GTM_LOCKMODE_WRITE))
#define MemoryContextUnlock(context) (GTM_RWLockRelease(&((MemoryContextData*)(context))->lock))
/*
 * MemoryContextIsValid
 *		True iff memory context is valid.
 *
 * Add new context types to the set accepted by this macro.
 */
#define MemoryContextIsValid(context) ((context) != NULL)

#endif /* MEMNODES_H */
